package com.vincent.kdp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.vincent.kdp.R;
import com.vincent.kdp.ui.bean.response.UncheckGoodsListOutput;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/18 0018.
 */

public class CartExpandableListAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<UncheckGoodsListOutput.Data> mListGoods = new ArrayList<>();
    public CartExpandableListAdapter(Context context) {
        mContext = context;
    }

    public void setList(List<UncheckGoodsListOutput.Data> mListGoods) {
        this.mListGoods = mListGoods;
//        setSettleInfo();
    }
    @Override
    public int getGroupCount() {
        return mListGoods.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mListGoods.get(groupPosition).getQueryList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mListGoods.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mListGoods.get(groupPosition).getQueryList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final GroupViewHolder holder;
        if (convertView == null) {
            holder = new GroupViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_expand_group, parent, false);
            holder.category = (TextView) convertView.findViewById(R.id.category_name);
            holder.totalNum = (TextView) convertView.findViewById(R.id.totalNum);
            holder.totalPrice = (TextView) convertView.findViewById(R.id.totalPrice);
            holder.allTotalWeight = (TextView) convertView.findViewById(R.id.allTotalWeight);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        holder.category.setText(mListGoods.get(groupPosition).getCategory());
        holder.totalNum.setText("总数量(件):"+mListGoods.get(groupPosition).getTotalNum());
        holder.totalPrice.setText("总价格(元):"+mListGoods.get(groupPosition).getTotalPrice());
        holder.allTotalWeight.setText("总克重(克):"+mListGoods.get(groupPosition).getAllTotalWeight());
        return convertView;
    }
    class GroupViewHolder {
        TextView category;
        TextView totalNum;
        TextView totalPrice;
        TextView allTotalWeight;

    }
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final  ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_item_uncheck_goods_list, parent, false);
            holder.mName = (TextView) convertView.findViewById(R.id.name);
            holder.mBarCode = (TextView) convertView.findViewById(R.id.bar_code);
            holder.mChargeUnit = (TextView) convertView.findViewById(R.id.charge_unit);

            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        UncheckGoodsListOutput.Data.QueryList entity=mListGoods.get(groupPosition).getQueryList().get(childPosition);
        System.out.println("uuuuuuuuuuuuuuentity.getName()-----"+entity.getName());
        holder.mName.setText(entity.getName());
        holder.mBarCode.setText(entity.getBarCode());
        if("1".equals(entity.getTotalUnit())){
            holder.mChargeUnit.setText("单/克："+entity.getTotalWeight()+"g");
        }else{
            holder.mChargeUnit.setText("单/克：¥"+entity.getPrice()+"元");
        }

        return convertView;
    }

    class ChildViewHolder {
        TextView mChargeUnit;
        TextView mName;
        TextView mBarCode;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
